﻿using System;
using System.Windows.Forms;
using mfe.bll;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Data;
using System.Text;

namespace mfe_proj.Forms
{
    ///TODO: should multi-thread this form.
    public partial class PersonForm : BaseDetailForm
    {
        public int AuthorID = 0;
        public ItemPerson person = null;
        string _ItemGuidID;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }

        public PersonForm()
        {
            InitializeComponent();
        }

        private void AuthorForm_Load(object sender, EventArgs e)
        {
            //LoadData();
            //LoadPhoto();
        }

        void LoadCountryList()
        {
            if (Program.CountryCollection == null) Program.CountryCollection = Country.CountryCollection(null, null);

            cbCountry.DataSource = Program.CountryCollection;
            cbCountry.DisplayMember = "Name";
            cbCountry.ValueMember = "ID";
            cbCountry.SelectedIndex = -1;
        }

        public void LoadData()
        {
            acPerson.ItemGuidID = ItemGuidID;
            acWork.ItemGuidID = ItemGuidID;
            LoadCountryList();

            if (Program.GenderTable == null)
            {
                Program.GenderTable = new System.Data.DataTable();
                Program.GenderTable.Columns.Add("ID", typeof(Int16));
                Program.GenderTable.Columns.Add("Name", typeof(string));
                DataRow row1 = Program.GenderTable.NewRow();
                DataRow row2 = Program.GenderTable.NewRow(); 
                row1["ID"] = 0;
                row1["Name"] = "Female";
                row2["ID"] = 1;
                row2["Name"] = "Male";
                Program.GenderTable.Rows.Add(row1);
                Program.GenderTable.Rows.Add(row2);
                Program.GenderTable.AcceptChanges();
            }

            cbGender.DataSource = Program.GenderTable;
            cbGender.DisplayMember = "Name";
            cbGender.ValueMember = "ID";
            cbGender.SelectedIndex = -1;

            //edit
            if (ItemGuidID != null && ItemGuidID != string.Empty)
            {
                //general
                person = new ItemPerson(new Guid(ItemGuidID));
                txtFirstName.Text = person.FirstName;
                txtMiddleName.Text = person.MiddleName;
                txtLastName.Text = person.LastName;
                txtChineseName.Text = person.ChineseName;
                txtFirstNameReal.Text = person.FirstNameReal;
                txtMiddleNameReal.Text = person.MiddleNameReal;
                txtLastNameReal.Text = person.LastNameReal;
                txtChineseNameReal.Text = person.ChineseNameReal;
                txtPenNames.Text = person.PenNames;
                ymdDob.Year = (int?)person.DobYear;
                ymdDob.Month = (int?)person.DobMonth;
                ymdDob.Day = (int?)person.DobDay;
                ymdDod.Year = (int?)person.DodYear;
                ymdDod.Month = (int?)person.DodMonth;
                ymdDod.Day = (int?)person.DodDay;
                cbCountry.SelectedValue = person.CountryID;
                cbGender.SelectedIndex = (int)person.Gender;
                //general (2)
                txtFirstAppear.Text = person.AppearYear > 0 ? person.AppearYear.ToString() : string.Empty;
                txtVirginWork.Text = person.VirginWork;
                txtDescending.Text = person.Descend;
                txtOccupation.Text = person.Occupation;
                txtRepresentativeWorks.Text = person.MainWork;
                txtInterest.Text = person.Interest;
                txtEducation.Text = person.Education;
                txtAwards.Text = person.Awards;

                //contact
                txtEmail.Text = person.Email;
                txtWebsiteUrl.Text = person.WebsiteUrl;
                txtBlogUrl.Text = person.BlogUrl;
                txtAddress1.Text = person.Address1;
                txtAddress2.Text = person.Address2;
                txtCity.Text = person.City;
                txtStateProvince.Text = person.StateProvince;
                txtPostalCode.Text = person.PostalCode;
                txtQQ.Text = person.QQ;
                txtMSN.Text = person.MSN;
                txtContactOther.Text = person.ContactNote;
                //note
                txtNote.Text = person.Note;
            }
        }

        public override string ValidateData()
        {
            StringBuilder sb = new StringBuilder();
            if (cbCountry.SelectedIndex == -1)
                sb.AppendLine("Country needs to be selected.");
            if (cbGender.SelectedIndex == -1)
                sb.AppendLine("Gender needs to be selected.");
            if (txtFirstAppear.Text.Trim().Length > 0)
            {
                try
                {
                    int.Parse(txtFirstAppear.Text.Trim());
                }
                catch
                {
                    sb.AppendLine("First appear must be numeric value.");
                }
            }

            return sb.ToString();
        }

        public override bool SaveData(string itemGuidID)
        {
            bool retval = false;
            try
            {
                //general
                if (person == null) person = new ItemPerson();
                person.ItemGuidID = itemGuidID;
                person.FirstName = txtFirstName.Text.Trim();
                person.MiddleName = txtMiddleName.Text.Trim();
                person.LastName = txtLastName.Text.Trim();
                person.ChineseName = txtChineseName.Text.Trim();
                person.PenNames = txtPenNames.Text.Trim();
                person.FirstNameReal = txtFirstNameReal.Text.Trim();
                person.MiddleNameReal = txtMiddleNameReal.Text.Trim();
                person.LastNameReal = txtLastNameReal.Text.Trim();
                person.ChineseNameReal = txtChineseNameReal.Text.Trim();
                if (ymdDob.Year.HasValue)
                    person.DobYear = ymdDob.Year.Value;
                if (ymdDob.Month.HasValue)
                    person.DobMonth = ymdDob.Month.Value;
                if (ymdDob.Day.HasValue)
                    person.DobDay = ymdDob.Day.Value;

                if (ymdDod.Year.HasValue)
                    person.DodYear = ymdDod.Year.Value;
                if (ymdDod.Month.HasValue)
                    person.DodMonth = ymdDod.Month.Value;
                if (ymdDod.Day.HasValue)
                    person.DodDay = ymdDod.Day.Value;
                person.PersonGuidID = Guid.NewGuid().ToString();
                person.CountryID = int.Parse(cbCountry.SelectedValue.ToString());
                person.Gender = ((System.Data.DataRowView)cbGender.SelectedItem)["Name"].ToString() == "Male" ? 1 : 0;
                //general (2)
                if (txtFirstAppear.Text.Trim().Length > 0)
                    person.AppearYear = int.Parse(txtFirstAppear.Text.Trim());
                person.VirginWork = txtVirginWork.Text.Trim();
                person.Descend = txtDescending.Text.Trim();
                person.Occupation = txtOccupation.Text.Trim();
                person.MainWork = txtRepresentativeWorks.Text.Trim();
                person.Interest = txtInterest.Text.Trim();
                person.Education = txtEducation.Text.Trim();
                person.Awards = txtAwards.Text.Trim();

                //contact
                person.Email = txtEmail.Text.Trim();
                person.WebsiteUrl = txtWebsiteUrl.Text.Trim();
                person.BlogUrl = txtBlogUrl.Text.Trim();
                person.Address1 = txtAddress1.Text.Trim();
                person.Address2 = txtAddress2.Text.Trim();
                person.City = txtCity.Text.Trim();
                person.StateProvince = txtStateProvince.Text.Trim();
                person.PostalCode = txtPostalCode.Text.Trim();
                person.QQ = txtQQ.Text.Trim();
                person.MSN = txtMSN.Text.Trim();
                person.ContactNote = txtContactOther.Text.Trim();
                //note
                person.Note = txtNote.Text.Trim();
                //save
                retval = (person.AuthorID == 0) ? person.Save() : person.Update();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return retval;
        }

        public Panel DockablePanel
        {
            get { return panel1; }
            set { panel1 = value; }
        }

        private void lbAddNewCountry_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Forms.CountryForm countryForm = new CountryForm();
            countryForm.CountryAdded += new CountryFormEventHandler(countryForm_CountryAdded);
            countryForm.ShowDialog();
        }

        void countryForm_CountryAdded(object sender, EventArgs e)
        {
            //reload the country list
            if (Program.CountryCollection != null)
            {
                Program.CountryCollection.Clear();
                Program.CountryCollection = null;
            }
            LoadCountryList();
        }
    }
}
